package com.bilibili.juc.cf.one;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

public class CompletableFutureOfOrTimeout {
    public static void main(String[] args) {
        // 4.10. 超时和取消
        CompletableFuture<String> timeoutFuture = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return "TIMEOUT";
        }).orTimeout(1, TimeUnit.SECONDS);


        CompletableFuture<String> exceptionally = timeoutFuture.exceptionally(ex -> {
            ex.printStackTrace();
            System.out.println("超时异常: " + ex.getMessage());
            return "超时异常设置的返回值";
        });

        System.out.println("finally result==> " + exceptionally.join());
    }
}
/*
超时异常: null
finally result==> 超时异常设置的返回值
 */
